﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.IViews.Admin;
using Portfolio.IViews.Common;
using Portfolio.BL.ArticleCategories;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Presenters.Interfaces;
using Portfolio.BL.Common;
using Portfolio.BL.Dictionary;

namespace Portfolio.Presenters.Admin
{

    /// <summary>
    /// Klasa presenter formularz do modyfikacji ArticleCategory
    /// </summary>
    public class AdminEditArticleCategoryPresenter : IDefaultPresenter
    {
        
        private IAdminEditArticleCategoryView _view;        
        private ArticleCategoryOperBL _articleCategoryOperBL;        

        /// <summary>
        /// Konstruktor presenter.
        /// </summary>
        /// <param name="view">Interfejs widoku strony</param>
        public AdminEditArticleCategoryPresenter(IAdminEditArticleCategoryView view)
        {
            _view = view;
            _articleCategoryOperBL = new ArticleCategoryOperBL(_view.IdArticleCategory);            
            this.Initialize();
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Inicjalizuje wszystkie zdarzenia na stronie
        /// </summary>
        public void Initialize()
        {
            this._view.LoadArticleCategory += new EventHandler<EventArgs>(_viewLoadArticleCategory);
            this._view.LoadFieldsDropDownList += new EventHandler<EventArgs>(_viewLoadFieldsDropDownList);           
            this._view.UpdateArticleCategory += new EventHandler<EventArgs>(_viewUpdateArticleCategory);
            this._view.InsertArticleCategory += new EventHandler<EventArgs>(_viewInsertArticleCategory);           
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie wykonuje wczytanie danych do kontrolki DetailsView (formularz)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewLoadArticleCategory(object sender, EventArgs e)
        {
            ArticleCategoriesBL articleCategoriesBL = new ArticleCategoriesBL();

            _view.ArticleCategoryDataBind = _articleCategoryOperBL.ArticleCategory;
            //brak możliwosći zmiany kategorii nadrzędnej, jeżeli kat. przypisane są już inne kategorie
            if (_articleCategoryOperBL.ArticleCategory != null) 
            {
                _view.DDLArticleCategoriesEnabled = articleCategoriesBL.ReturnDDLArticleCategoryEnabled(_articleCategoryOperBL.ArticleCategory.ArticleCategoryID);
            }

            _view.PublishException = _articleCategoryOperBL.InformationException;
        }

        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie uzupełnia pola DropDownList AticleCategories i Status
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewLoadFieldsDropDownList(object sender, EventArgs e)
        {
            ArticleCategoriesDDLBL articleCategoriesDDLBL = new ArticleCategoriesDDLBL();

            if (_articleCategoryOperBL.ArticleCategory == null)
            {
                //nowy artykuł
                _view.ArticleCategoriesDataSource = new DataSourceContainer("Name", "ArticleCategoryID", articleCategoriesDDLBL.ReturnArticleCategoriesDDLForm(), null);
                _view.StatusDataSource = new DataSourceContainer("Value", "Key", CommonDictionary.Instance.GetStatusDictionary(), null);            
            }
            else
            {
                //modyfikacja artykułu
                _view.ArticleCategoriesDataSource = new DataSourceContainer("Name", "ArticleCategoryID", articleCategoriesDDLBL.ReturnArticleCategoriesDDLFormByNotArticleCategoryID(_articleCategoryOperBL.ArticleCategory.ArticleCategoryID), _articleCategoryOperBL.ArticleCategory.ArticleCategoryIDID);
                _view.StatusDataSource = new DataSourceContainer("Value", "Key", CommonDictionary.Instance.GetStatusDictionary(), _articleCategoryOperBL.ArticleCategory.Status);            
            }            
            
            _view.PublishException = _articleCategoryOperBL.InformationException;
        }       

        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie modyfikuje ArticleCategory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewUpdateArticleCategory(object sender, EventArgs e)
        {
            _view.IdArticleCategory = _articleCategoryOperBL.UpdateArticleCategory(_view.ArticleCategoriesData);
            
            _view.PublishInformationSuccess = _articleCategoryOperBL.InformationSuccess;
            _view.PublishException = _articleCategoryOperBL.InformationException;                       
        }

        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie dodaje nową ArticleCategory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewInsertArticleCategory(object sender, EventArgs e)
        {
            _view.IdArticleCategory = _articleCategoryOperBL.InsertArticleCategory(_view.ArticleCategoriesData);
            _view.PublishInformationSuccess = _articleCategoryOperBL.InformationSuccess;
            _view.PublishException = _articleCategoryOperBL.InformationException;         
        }
        
    }
}

